<?php
/**
 *	Collection of SQL Statements for Bug Search.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Collection of SQL Statements for Bug Search.
 *	@package		tools.BugTracker
 *	@extends		Database_StatementCollection
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
final class Collection_Bug extends Database_StatementCollection
{
	/**
	 *	Basic SQL Components.
	 *	@access		public
	 *	@return		array
	 */
	public function collectBugs()
	{
		$keys	= array(
			"b.bugId",
			"b.serverId",
			"b.projectId",
			"b.location",
			"b.function",
			"b.title",
			"b.description",
			"b.author",
			"b.created",
			"b.modified",
			"b.status",
			"b.type",
			"b.priority",
			"s.title as server",
			"p.title as project",
		);
		$tables	= array(
			"bugs as b",
			"projects as p",
			"servers as s",
		);
		$conditions	= array(
			"b.serverId=s.serverId",
			"b.projectId=p.projectId",
		);
		$this->builder->addKeys( $keys );
		$this->builder->addTables( $tables );
		$this->builder->addConditions( $conditions );
	}


	/**
	 *	Bug.
	 *	@access		public
	 *	@param		int			$bug_id			ID of Bug
	 *	@return		array
	 */
	public function withId( $bug_id )
	{
		$this->builder->addCondition( "b.bug_id=".$bug_id );
	}

	/**
	 *	Priority.
	 *	@access		public
	 *	@param		array		$priorities		List of Priorities
	 *	@return		array
	 */
	public function withPriorities( $priorities )
	{
		$list		= array();
		$prios	= (array) $prios;
		foreach( $priorities as $priority )
			$list[]	= "b.priority=".$priority;
		$condition	= "(".implode( " OR ", $list ).")";
		$this->builder->addCondition( $condition );
	}

	/**
	 *	Project.
	 *	@access		public
	 *	@param		array		$projects		Array of Projects
	 *	@return		array
	 */
	public function withProject( $projects )
	{
		$list		= array();
		$projects	= (array) $projects;
		foreach( $projects as $project )
			$list[]	= "b.project=".$project;
		$projects	= "(".implode( " OR ", $list ).")";
		$this->builder->addCondition( $projects );
	}

	/**
	 *	Server.
	 *	@access		public
	 *	@param		array		$servers		Array of Servers
	 *	@return		array
	 */
	public function withServers( $servers )
	{
		$list		= array();
		$servers	= (array) $servers;
		foreach( $servers as $server )
			$list[]	= "b.server=".$server;
		$servers	= "(".implode( " OR ", $list ).")";
		$this->builder->addCondition( $servers );
	}

	/**
	 *	Status.
	 *	@access		public
	 *	@param		array		$states			List of States
	 *	@return		array
	 */
	public function withStates( $states )
	{
		$list		= array();
		$states		= (array) $states;
		foreach( $states as $state )
			$list[]	= "b.status=".$state;
		$condition	= "(".implode( " OR ", $list ).")";
		$this->builder->addCondition( $condition );
	}

	/**
	 *	Title.
	 *	@access		public
	 *	@param		string		$title			Title to be like
	 */
	public function withTitleLike( $title )
	{
		$conditions	= array(
			"b.title LIKE '%".$title."%'",
			"b.description LIKE '%".$title."%'",
		);
		$condition	= "(".implode( " OR ", $conditions ).")";
		$this->builder->addCondition( $condition );
	}

	/**
	 *	Type.
	 *	@access		public
	 *	@param		array		$types			Array of Types
	 *	@return		array
	 */
	public function withTypes( $types )
	{
		$list	= array();
		$types	= (array) $types;
		foreach( $types as $type )
			$list[]	= "b.type=".$type;
		$condition	= "(".implode( " OR ", $list ).")";
		$this->builder->addCondition( $condition );
	}
	
	/**
	 *	Lastest.
	 *	@access		public
	 *	@param		int			$amount			Amount to limit to
	 */
	public function limitLatest( $amount )
	{
		$this->setLimit( 0, $amount );
		$this->orderBy( 'modified', "DESC" );
	}

	/**
	 *	Developer.
	 *	@access		public
	 *	@param		int			$user_id		ID of Developer
	 *	@return		array
	 */
	public function withDeveloper( $user_id )
	{
		$tables	= array(
			"assignments as a",
			"userroles as ur",
			);
		$conditions	= array(
			"b.bug_id=a.bug_id",
			"a.user_id=".$user_id,
			"a.user_id=ur.user_id",
			"ur.role_id=3",
		);
		$this->builder->addTables( $tables );
		$this->builder->addConditions( $conditions );
	}

	/**
	 *	Author.
	 *	@access		public
	 *	@param		int			$user_id		ID of Author
	 *	@return		array
	 */
	public function withAuthor( $user_id )
	{
		$condition = "author=".$user_id;
		$this->builder->addCondition( $condition );
	}

	public function nextBug( $bug_id )
	{
		$condition = "bug_id>".$bug_id;
		$this->setLimit( 0, 1 );
		$this->orderBy( 'bug_id', "ASC"  );
		$this->builder->addCondition( $condition );
	}
	
	public function previousBug( $bug_id )
	{
		$condition = "bug_id<".$bug_id;
		$this->setLimit( 0, 1 );
		$this->orderBy( 'bug_id', "DESC" );
	}
}
?>